#! /usr/bin/python

import os,sys,re

boxes = [("/Users/ajeet/Mail/.Gmail", "Gmail", "G"), ("/Users/ajeet/Mail/.SFU", "SFU", "S"), ("/Users/ajeet/Mail/.FAS", "FAS", "F"), ("/Users/ajeet/Mail/.Gmail.Zombies", "Zombies", "Z")]

message = ""
type = 0 #0 = long, 1 = one line
msg_len = {"FROM" : 30, "SUBJECT" : 30, "BODY" : 50}
if len(sys.argv) > 1:
    if sys.argv[1] == "-1":
        type = 1

if len(sys.argv) > 1:
    if sys.argv[1] == "-0":
        type = 0

if type == 1:
    message = "[ "

for box, name, sname in boxes:
    if not os.path.exists(os.path.join(box,"new")):
        continue
    files = os.listdir(os.path.join(box,"new"))
    if len(files) > 0:
        if type == 0:
            message += name+": "+str(len(files))+" new\n"
            for fil in files:
                f = open(os.path.join(box, "new", fil), "r")
                lines = f.readlines()
                msg = {}
                msg["FROM"] = [l[5:].strip() for l in lines if re.search("^from:", l.lower())][0][0:msg_len["FROM"]]
                msg["SUBJECT"] = [l[8:].strip() for l in lines if re.search("^subject:", l.lower())][0][0:msg_len["SUBJECT"]]
                bodyidx=0
                for i in range(len(lines)):
                    if lines[i] == "\n":
                        bodyidx = i+1
                        break
                msg["BODY"] = "".join(lines[bodyidx:]).replace("\n","").strip()[0:msg_len["BODY"]]
                base_message = "\t%(FROM)-"+str(msg_len["FROM"])+"s | %(SUBJECT)-"+str(msg_len["SUBJECT"])+"s | %(BODY)-"+str(msg_len["BODY"])+"s\n"
                message += base_message % msg
                f.close()
        if type == 1:
            message += sname+": "+str(len(files))+" "

if type == 1:
    message = message + "]"
    if message == "[ ]":
        message = ""

if type == 0 and message == "":
    message = "No new messages"
print message.strip(),
